package com.lxmes.hr.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcAbnormalAttend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcPieceworkMoneySummaryMapper;
import com.lxmes.hr.domain.NcPieceworkMoneySummary;
import com.lxmes.hr.service.INcPieceworkMoneySummaryService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Validator;

/**
 * 计件工资汇总Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-16
 */
@Service
public class NcPieceworkMoneySummaryServiceImpl implements INcPieceworkMoneySummaryService
{
    @Autowired
    private NcPieceworkMoneySummaryMapper ncPieceworkMoneySummaryMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询计件工资汇总
     *
     * @param id 计件工资汇总主键
     * @return 计件工资汇总
     */
    @Override
    public NcPieceworkMoneySummary selectNcPieceworkMoneySummaryById(Long id)
    {
        return ncPieceworkMoneySummaryMapper.selectNcPieceworkMoneySummaryById(id);
    }

    /**
     * 查询计件工资汇总列表
     *
     * @param ncPieceworkMoneySummary 计件工资汇总
     * @return 计件工资汇总
     */
    @Override
    public List<NcPieceworkMoneySummary> selectNcPieceworkMoneySummaryList(NcPieceworkMoneySummary ncPieceworkMoneySummary)
    {
        return ncPieceworkMoneySummaryMapper.selectNcPieceworkMoneySummaryList(ncPieceworkMoneySummary);
    }

    /**
     * 新增计件工资汇总
     *
     * @param ncPieceworkMoneySummary 计件工资汇总
     * @return 结果
     */
    @Override
    public int insertNcPieceworkMoneySummary(NcPieceworkMoneySummary ncPieceworkMoneySummary)
    {
        ncPieceworkMoneySummary.setCreateTime(DateUtils.getNowDate());
        return ncPieceworkMoneySummaryMapper.insertNcPieceworkMoneySummary(ncPieceworkMoneySummary);
    }

    /**
     * 修改计件工资汇总
     *
     * @param ncPieceworkMoneySummary 计件工资汇总
     * @return 结果
     */
    @Override
    public int updateNcPieceworkMoneySummary(NcPieceworkMoneySummary ncPieceworkMoneySummary)
    {
        ncPieceworkMoneySummary.setUpdateTime(DateUtils.getNowDate());
        return ncPieceworkMoneySummaryMapper.updateNcPieceworkMoneySummary(ncPieceworkMoneySummary);
    }

    /**
     * 批量删除计件工资汇总
     *
     * @param ids 需要删除的计件工资汇总主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkMoneySummaryByIds(Long[] ids)
    {
        return ncPieceworkMoneySummaryMapper.deleteNcPieceworkMoneySummaryByIds(ids);
    }

    /**
     * 删除计件工资汇总信息
     *
     * @param id 计件工资汇总主键
     * @return 结果
     */
    @Override
    public int deleteNcPieceworkMoneySummaryById(Long id)
    {
        return ncPieceworkMoneySummaryMapper.deleteNcPieceworkMoneySummaryById(id);
    }

    @Override
    public String imporIteminfo(List<NcPieceworkMoneySummary> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcPieceworkMoneySummary org : userList)
        {
            try
            {
                BeanValidators.validateWithException(validator, org);
                org.setCreateBy(operName);
                org.setUpdateBy(operName);
                this.insertNcPieceworkMoneySummary(org);
                successNum++;
                successMsg.append("<br/>" + successNum + "、员工工号 " + org.getJobNo() + " 导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + org.getJobNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int moneySummary() {
        ncPieceworkMoneySummaryMapper.deleteCurrentMonthSummary();
        String currentMonthOfFirstDay = DateUtils.getFirstDayOfMonth();
        List<NcPieceworkMoneySummary> ncPieceworkMoneySummaries = ncPieceworkMoneySummaryMapper.moenyDetailSummary(currentMonthOfFirstDay);
        for(NcPieceworkMoneySummary summary : ncPieceworkMoneySummaries){
            summary.setCreateTime(new Date());
            String username = SecurityUtils.getUsername()==null?"":SecurityUtils.getUsername();
            if(StringUtils.isEmpty(username)){
                summary.setCreateBy("系统");
            }else{
                summary.setCreateBy(username);
            }
            ncPieceworkMoneySummaryMapper.insertNcPieceworkMoneySummary(summary);
        }
        return 1;
    }
}
